package com.jiefutong.zyt;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.google.zxing.common.StringUtils;
import com.jiefutong.zyt.bean.LoginBean;
import com.jiefutong.zyt.utils.PrefsUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.zhy.autolayout.AutoLayoutActivity;

public class LoginActivity extends AutoLayoutActivity {
    private EditText accountET;
    private EditText passwordET;
    private Button loginBtn;
    private Button registerBtn;

    private ImageView clearIV;
    private CheckBox hidepassword;

    private TextView forgetTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });


        hidepassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int passwordLength = passwordET.getText().length();
                if(isChecked){
                    passwordET.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }else{
                    passwordET.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                passwordET.setSelection(passwordLength);
            }
        });

        clearIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                accountET.setText(null);

            }
        });

        registerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });

        forgetTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,BackPassordActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        accountET=findViewById(R.id.login_accountET);
        passwordET=findViewById(R.id.login_passwordET);
        loginBtn=findViewById(R.id.login_loginBtn);
        clearIV=findViewById(R.id.login_clearIV);
        hidepassword=findViewById(R.id.login_hidepassword);
        registerBtn=findViewById(R.id.login_registerBtn);
        forgetTV=findViewById(R.id.login_forgetTV);
    }

    public void login(){

        if(TextUtils.isEmpty(accountET.getText().toString())){
            Toast.makeText(getBaseContext(), "请输入您的账号", Toast.LENGTH_LONG).show();
            return;
        }

        if(TextUtils.isEmpty(passwordET.getText().toString())){
            Toast.makeText(getBaseContext(), "请输入您的密码", Toast.LENGTH_LONG).show();
            return;
        }
        showProgress(true);
        HttpParams params = new HttpParams();
        params.put("userMobile", accountET.getText().toString());
        params.put("password", passwordET.getText().toString());
        OkGo.<String>post("https://zyt.jiefutong.net/api/index/api/login")
                .tag(this)
                .params(params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        final LoginBean bean = JSON.parseObject(response.body(), LoginBean.class);
                        if (bean != null && bean.status.equals("success")) {
                            Toast.makeText(getBaseContext(),"登录成功", Toast.LENGTH_SHORT).show();
                            PrefsUtil.putString(LoginActivity.this, "token", bean.data.token_type+" "+bean.data.access_token);
                            //Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                            Intent intent=new Intent(LoginActivity.this,TabActivity.class);
                            startActivity(intent);
                        }else{
                            Toast.makeText(getBaseContext(),bean.message, Toast.LENGTH_SHORT).show();
                        }
                        showProgress(false);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        showProgress(false);
                    }
                });
    }

    /**
     * Shows the progress UI and hides the login form.
     */
    private ProgressDialog progress;
    public void showProgress(final boolean show) {
        if (progress == null) {
            progress = new ProgressDialog(
                    LoginActivity.this);
           // progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条
           progress.setMessage("正在加载...");
            progress.setCanceledOnTouchOutside(false);
        }

        if (show) {
            progress.show();
        } else {
            progress.dismiss();
        }
    }
}
